﻿
#include "FrameWindow.h"

CVerticalLayoutUI * g_pLayout = NULL;
CScrollBarUI * g_pVerticalSrcollBar = NULL;

CActiveXUI * g_pActiveX = NULL;

CFrameWindow::CFrameWindow()
{
    m_pMenUI = NULL;
};

CFrameWindow::~CFrameWindow()
{
	if (m_pMenUI)
	{
		m_pMenUI->Destroy();
		delete m_pMenUI;
		m_pMenUI = NULL;
	}
};

void CFrameWindow::OnNotify(MUINOTIFY &notify)
{
	//想知道wParam, lParam对应了什么参数吗？说实话，由于项目还处于初期，
	//我也说不准wParam, lParam到底对应了什么参数，自己在断点中拦截到对应
	//的通知后，看[调用堆栈]追踪一下吧。
	CControlUI * pControl = (CControlUI*) notify.pSender;
	switch(notify.dwType)
	{
	case EnumNotify::ClickItem:
	{
        if (CButtonUI::g_strClassName == pControl->GetClassName())
	    {
		    //MessageBox(m_hWnd, pConntrol->GetText(), _T("你点击了："), NULL);
			//if (m_pMenUI && pControl->GetName() == _T("btnMenu"))
			//{
			//	m_pMenUI->Popup(NULL, CMuiRect(400, 400, 400, 400));
			//}

			HDC hDC = ::GetDC(NULL);
			if (hDC)
			{
				int nDpiX = ::GetDeviceCaps(hDC, LOGPIXELSX);
				int nDpiY = ::GetDeviceCaps(hDC, LOGPIXELSY);
				::ReleaseDC(NULL, hDC);
				hDC = NULL;
			}

	    }
	}break;
	case EnumNotify::ActiveItem:
		{
			pControl = (CControlUI*)notify.lParam;
			//MessageBox(m_hWnd, pConntrol->GetClassName(), pConntrol->GetText(), NULL);
		}break;
	case EnumNotify::TimerCall:
		{
			//pControl->KillTimer(10);
			//MessageBox(m_hWnd, pControl->GetClassName(), pControl->GetName(), NULL);
		}break;
	case EnumNotify::CheckItem:
		{
			CControlUI * pShow = NULL;
			if(FALSE == notify.wParam)
			{
				//FALSE = 取消选中
				//TRUE = 选中
				break;
			}

            if (pControl->GetName() == _T("optLv"))
			{
				pShow = FindControl(_T("ListView"));
			}
            else if (pControl->GetName() == _T("optAbs"))
			{
				pShow = FindControl(_T("Absolute"));
			}
            else if (pControl->GetName() == _T("optFilm"))
			{
				pShow = FindControl(_T("Film"));
			}
            else if (pControl->GetName() == _T("optWeb"))
			{
				pShow = FindControl(_T("WebBrowser"));
			}

			if(pShow)
			{
				//::MessageBox(m_hWnd, pConntrol->GetName(), pConntrol->GetClassName(), NULL);
				CTabLayoutUI * pTab = static_cast<CTabLayoutUI *>(pShow->GetParent());
				pTab->SelectItem(pShow);
			}
		}break;
	}

	//return __super::OnNotify(notify);
};

LRESULT CFrameWindow::OnEvent(MUIEVENT &event)
{
	CDragItemUI * pDragItem = NULL;
	switch (event.dwType)
	{
	case EnumEvent::WindowInit:
		{
			OnCreate(event.wParam, event.lParam);
		}break;
	case EnumEvent::WindowReady:
		{
			OnReady(event.wParam, event.lParam);
		}break;
	case EnumEvent::DragOver:
		{
			pDragItem = (CDragItemUI *)event.wParam;
			pDragItem->Destroy();
			delete pDragItem;

		}break;
	case EnumEvent::OnTimer:
		{
			::KillTimer(m_hWnd, event.wParam);
			CContainerUI * pLayout = (CContainerUI*)GetRootControl();
			static CControlUI * pControl = NULL;
			switch(event.wParam)
			{
			case 1:
				{
					pControl = pLayout->Find((int) 1);
					pLayout->Remove(pControl);

					::SetTimer(m_hWnd, 2, 5000, NULL);
				}break;
			case 2:
				{
					pLayout->Add(pControl);
				}break;
			}
			
			pLayout->Update();
			
			//this->Close();
		}break;
	case EnumEvent::WindowDestroy:
		{
			if (m_pMenUI)
			{
				m_pMenUI->Destroy();
				delete m_pMenUI;
				m_pMenUI = NULL;
			}
			SetSkin(NULL);
			//PostQuitMessage(0);
		}break;
	default:
		break;
	}

	return 0;
	//return __super::OnEvent(event);
}

LRESULT CALLBACK CFrameWindow::WndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	CControlUI * pControl = NULL;
	POINT point;
	CDragItemUI * pDragItem = NULL;
	SIZE szRound = {10,10};
	BYTE keyState[256] = { 0 };
	switch (message)
	{
	case WM_LBUTTONDOWN:
		{
			point.x = GET_X_LPARAM(lParam);
			point.y = GET_Y_LPARAM(lParam);
		}break;
	case WM_MOUSEWHEEL:
	{
		if (FALSE == GetKeyboardState(keyState))
		{
			break;
		}

		if (keyState[VK_CONTROL] & 128)
		{
			int nRatio = this->GetDpiRatio();
			int nShift = (short)HIWORD(wParam);

			nRatio += nShift / 30;
			this->SetDpiRatio(nRatio);
			::UpdateWindow(m_hWnd);
			::InvalidateRect(m_hWnd, NULL, FALSE);
			return 0;
		}
		
	}break;
	default:
		break;
	}
	return __super::WndProc(message, wParam, lParam);
}

LRESULT CFrameWindow::OnTimer(WPARAM wParam, LPARAM lParam)
{
	return 0;
}

LRESULT CFrameWindow::OnCreate2(WPARAM wParam, LPARAM lParam)
{
	TCHAR strSkin[MAX_PATH] = {0};

	wsprintf(strSkin, _T("file='%s'"), _T("skin/"));

	SetSkin(strSkin);
	//SetSkin(_T("skin\\"));

	CVerticalLayoutUI * pRootLayout = new CVerticalLayoutUI();
	pRootLayout->SetBkColor(ARGB(255,255,0,255));
	pRootLayout->SetBorderPen(2, ARGB(255,0,255,0));
	pRootLayout->SetInset(CMuiRect(100,100,100,100));

	CControlUI * pControl = new CControlUI();
	pControl->SetBkColor(ARGB(255,100,100,100));
	pRootLayout->Add(pControl);
	
	CRichEditUI * pRichEdit = new CRichEditUI();
	pRichEdit->SetBkColor(ARGB(255,255,255,255));
	pRootLayout->Add(pRichEdit);

	this->AttachFrameView(pRootLayout);
	
	return 0;
}

LRESULT CFrameWindow::OnCreate(WPARAM wParam, LPARAM lParam)
{
	TCHAR strSkin[MAX_PATH] = {0};
	wsprintf(strSkin, _T("file='%s'"), _T("skin/"));
	SetSkin(strSkin);
	//SetSkin(_T("skin\\"));

	CControlUI * pRootLayout = nullptr;

	CBuilder * pBuilder = new CBuilder(this, nullptr);
	pRootLayout = pBuilder->Create(strSkin, _T("frame.xml"));
	delete pBuilder;
	this->AttachFrameView(pRootLayout);
	

	CControlUI* pEdit = FindControl(_T("edtMessage"));
	if (pEdit)
	{
		//pEdit->SetText(_T("床前\t明月光\n疑是\t\t地上霜\n举头\t\t\t望明月\r\n低头\t\t\t\t思故乡"));
		pEdit->SetText(_T("床前\n明月光\r\n疑是\n\n地上霜\n举头\n望\n明月"));
		//pEdit->SetText(_T("床前\t明月光\r\n疑是\t\t地上霜\n"));
	}

	return 0;
}

LRESULT CFrameWindow::OnReady(WPARAM wParam, LPARAM lParam)
{
	return 0;
}